//package com.adingxiong.pm.realm;
//
//
//
//import com.adingxiong.pm.entity.Pmuser;
//import com.adingxiong.pm.server.IPmuserService;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.annotation.Resource;
//
//
///**
// * Realm中有3种验证的方式,InitRealm,JdbcRealm.和自定义的Realm.通常使用自定义的方式.
//   使用自定义的Realm继承AuthorizingRealm,并重写两个方法,一个是验证身份,一个是验证权限
// *
// */
//public class MyRealm extends AuthorizingRealm {
//
//    @Resource
//    private IPmuserService iPmuserService;
//    //引入日志输出
//    private Logger logger = LoggerFactory.getLogger(getClass());
//
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        logger.info("==========用户权限============");
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        //获取当前用户
//        String name = String.valueOf(SecurityUtils.getSubject().getPrincipal());
//        QueryWrapper<Pmuser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("name",name);
//        Pmuser user = iPmuserService.getOne(queryWrapper);
//        //查询用户权限
//        String role = user.getRole();
//        /*将role放在一个集合中,多个权限使用集合set*/
//        info.addRole(role);
//        return info;
//    }
//
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        logger.info("===========用户验证===========");
//        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
//        String username = token.getUsername();
//
//        QueryWrapper<Pmuser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("name" ,username);
//        Pmuser pmuser = iPmuserService.getOne(queryWrapper);
//
//        if(pmuser ==null){
//            throw new AuthenticationException("用户名不正确");
//        }else {
//            String pwd = pmuser.getPassword();
//            if(!pwd.equals(token.getPassword())){
//                throw new AuthenticationException("密码不正确");
//            }
//        }
//
//        //组合一个验证信息
//        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token.getPrincipal(),pmuser.getPassword(),getName());
//        return info;
//
//    }
//}
